package com.example.travelgd.controller;

import com.example.travelgd.entity.Food;
import com.example.travelgd.service.FoodService;
import com.example.travelgd.util.FwResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/food")
@Tag(name = "FoodController", description = "美食推荐管理")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @GetMapping("/list")
    @Operation(summary = "获取美食列表")
    public FwResult<List<Food>> getFoodList(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        try {
            List<Food> foodList = foodService.getFoodList(categoryId, page, pageSize);
            log.info("获取美食列表成功, 分类: {}, 数据: {}", categoryId, foodList);
            return FwResult.ok(foodList);
        } catch (Exception e) {
            log.error("获取美食列表失败", e);
            return FwResult.failedMsg("获取美食列表失败");
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取美食详情")
    public FwResult<Food> getFoodDetail(@PathVariable Long id) {
        try {
            Food food = foodService.getFoodDetail(id);
            log.info("获取美食详情成功, id: {}, 数据: {}", id, food);
            return FwResult.ok(food);
        } catch (Exception e) {
            log.error("获取美食详情失败, id: {}", id, e);
            return FwResult.failedMsg("获取美食详情失败");
        }
    }

    @PostMapping
    @Operation(summary = "添加美食")
    public FwResult<Void> addFood(@RequestBody Food food) {
        return foodService.addFood(food);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新美食")
    public FwResult<Void> updateFood(@PathVariable Long id, @RequestBody Food food) {
        food.setId(id);
        return foodService.updateFood(food);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除美食")
    public FwResult<Void> deleteFood(@PathVariable Long id) {
        return foodService.deleteFood(id);
    }
}